import calendar_ex.*;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

//
// Main class for the IS3 Calendar prototype
//
public class IS3Calendar extends JFrame {

    private CalendarEx calendar;
    private CalendarController controller;
    ObservingJPanel template;
    View previous = View.MONTH;

    public enum View {
        EVENT, DAY, WEEK, MONTH, YEAR, CREATE, FILTER
    }
    private View currentView;

    //
    // Application entry point
    //
    public static void main(String args[]) {
        // Make it go!
        IS3Calendar i = new IS3Calendar();
    }

    //
    // Constructor
    //
    public IS3Calendar() {
        // Create and load the calendar
        this.calendar = new CalendarEx();
        //this.calendar.openCalendar("/home/peter/is3-calendar/calendar.txt");
        this.calendar.openCalendar("calendar.txt");

        // DEBUG: Print the calendar.
        this.calendar.printCalendar();

        // ADDED BY PETER
        this.controller = new CalendarController(this.calendar, this);
        this.template = new MainTemplatePanel(controller, new MonthViewPanel(this.calendar, controller));
        this.add(template);

        this.setPreferredSize(new Dimension(300, 500));

        // Set our default view.
        this.currentView = View.MONTH;

        // Default to exiting on close
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Pack and display the window.
        this.pack();
        this.setVisible(true);
    }

    //
    // Changes the current view.
    //
    public void changeView(View view) {
        // Set previous and current view
        previous = currentView;
        currentView = view;

        // Change to requested view
        if (view == View.FILTER) {
            this.setDialogTemplateView("Filter", new FilterPanel(calendar, controller));

        } else if (view == View.MONTH) {
            this.setMainTemplateView(new MonthViewPanel(calendar, controller));

        } else if (view == View.EVENT) {
            this.setDialogTemplateView(calendar.getCurrentAppointment().name, new AddEventPanel(calendar.getCurrentAppointment(),controller));

        } else if (view == View.DAY) {
            this.setMainTemplateView(new DayViewPanel(calendar, controller));

        } else if (view == View.YEAR) {
            this.setMainTemplateView(new YearViewPanel(calendar, controller));

        } else if (view == View.CREATE) {
            this.setDialogTemplateView("Create Event", new AddEventPanel(null,controller));

        } else if (view == View.WEEK) {
            this.setMainTemplateView(new WeekViewPanel(calendar, controller));

        }

        // Update JFrame
        repaint();
    }

    public void changeToPreviousView() {
        changeView(this.previous);
    }

    public void zoomInView() {
        if (currentView == View.YEAR) {
            this.changeView(View.MONTH);
        } else if (currentView == View.MONTH) {
            this.changeView(View.WEEK);

        } else if (currentView == View.WEEK) {
            this.changeView(View.DAY);

        } else if (currentView == View.DAY) {
            // No action
        }
        System.out.println(currentView);
    }

    public void zoomOutView() {
        if (currentView == View.YEAR) {
            // No action
        } else if (currentView == View.MONTH) {
            this.changeView(View.YEAR);

        } else if (currentView == View.WEEK) {
            this.changeView(View.MONTH);

        } else if (currentView == View.DAY) {
            this.changeView(View.WEEK);

        }
        System.out.println(currentView);
    }

    private void setMainTemplateView(JPanel content) {
        this.template = new MainTemplatePanel(controller, content);
    }

    private void setDialogTemplateView(String title, JPanel content) {
        this.template = new DialogTemplatePanel(this.controller, title, content);
    }

    //
    // Getters
    //
    public CalendarEx getCalendar() {
        return this.calendar;
    }

    public View getCurrentView() {
        return this.currentView;
    }

    public ObservingJPanel getTemplateView() { return this.template; }

    @Override
    public void repaint() {
        super.repaint();
        setContentPane(template);
        validate();
    }
}


